<?php

namespace App\Biz;

use App\Models\VisitorApprovalLog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class VisitorApprovalLogBiz extends BaseBiz
{
    protected HttpBiz     $httpBiz;
    protected UploadedBiz $uploadedBiz;

    public function __construct(VisitorApprovalLog $visitorApprovalLog, HttpBiz $httpBiz, UploadedBiz $uploadedBiz)
    {
        $this->model       = $visitorApprovalLog;
        $this->httpBiz     = $httpBiz;
        $this->uploadedBiz = $uploadedBiz;
    }

    /**
     * @param array $params
     * @return VisitorApprovalLog|Model
     */
    public function create(array $params): Model|VisitorApprovalLog
    {
        return $this->model->create([
            "uniqueId"          => $params['uniqueId'] ?? '',
            "unionId"           => $params['unionId'] ?? '',
            "openid"            => $params['openid'] ?? '',
            "authorId"          => $params['authorId'] ?? '',
            "image_path"        => $params['image_path'] ?? '',
            "visitor_name"      => $params['visitorName'] ?? '',
            "visitor_phone"     => $params['telephone'] ?? '',
            "interviewee_name"  => $params['interviewee'] ?? '',
            "interviewee_phone" => $params['intervieweePhone'] ?? '',
            "reason"            => $params['reason'] ?? '',
            "author_status"     => $params['authorStatus'] ?? 0,
            "arrive_status"     => $params['arriveStatus'] ?? 0,
            "verify_status"     => $params['verifyStatus'] ?? 0,
            "pass_type"         => $params['passType'] ?? 0,
            "source"            => $params['source'] ?? 0,
            "start_time"        => $params['startTime'] ?? null,
            "end_time"          => $params['endTime'] ?? null,
            "created"           => $params['created'] ?? null,
            "updated"           => $params['updated'] ?? null,
        ]);
    }

    /**
     * 创建日志保存图片
     * @param Collection $visitorReservesList
     * @return Collection
     */
    public function createAndPutFile(Collection $visitorReservesList): Collection
    {
        return $visitorReservesList->filter(function ($item){

            return !empty($item['photoData']);
        })->map(function ($item) {

            $item['image_path_url'] = Str::replace('http://10.131.9.133:30089/', 'http://visitor.cmbl.com.cn/openresty/', $item['photoData']);
            if ($pathInfo = parse_url($item['image_path_url'])) {

                //获取图片
                $response = $this->httpBiz->image($item['image_path_url']);

                if ($response->successful() && $contents = $response->body()) {
                    //上传文件到本地
                    $this->uploadedBiz->putFileAs($item['image_path'] = $pathInfo['path'], $contents);
                }
            }
            return $item;
        })->map(function ($item) {
            //创建日志
            return $this->create($item);
        });
    }
}
